package com.bufpay.action;

import com.bean.ResponseBean;
import com.bufpay.pojo.PayModel;
import com.bufpay.util.PayUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/pay")
public class PayController {
	private static Logger logger = LogManager.getLogger();
	@RequestMapping("/pay")
	public ResponseBean pay(String price, String pay_type,String name) {
		PayModel payModel = new PayModel();
		payModel.setName(name);
		payModel.setPay_type(pay_type);
		payModel.setPrice(price);
		payModel.setOrder_id(PayUtil.getOrderIdByUUId());
		payModel.setOrder_uid("1000");
		payModel.setNotify_url(PayUtil.NOTIFY_URL);
		payModel.setReturn_url(PayUtil.RETURN_URL);
		payModel.setSign(PayUtil.getSign(payModel));
		logger.info("开始支付的Key：" + payModel.getSign());
		return new ResponseBean(200,"ok",payModel);
	}

	@RequestMapping("/notifyPay")
	public void notifyPay(String aoid,String order_id,String order_uid,String price,String pay_price,String sign,HttpServletResponse response) throws IOException {
		// 保证密钥一致性
		if (PayUtil.checkSign(aoid,order_id,order_uid,price,pay_price,sign)) {
			// TODO 支付成功的业务处理
			//这里业务处理
			//如果支付金额是前端传递过来的，这里还要验证支付金额
			//return  new ResponseBean(200,"恭喜支付成功",payModel.getOrder_no());
			System.out.println("签名通过1");
			response.getWriter().println("success");
		} else {
			// 签名错误
			System.out.println("签名失败");
			response.getWriter().println("签名错误");
		}
	}

}
